package com.yuexun.dhdoctor.widget.model;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.bean.model.HelpFileData;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenss on 2017/7/31 0031.
 */

public class CreateTxtView_Optional {

    Context context;
    ArrayList<HelpFileData> dtlList;//配置详情列表
    List<HelpFileData> alreadyDtlList;
    boolean isBackfill;

    public CreateTxtView_Optional(Context context, ArrayList<HelpFileData> dtlList, List<HelpFileData> alreadyDtlList, boolean isBackfill) {
        this.context = context;
        this.dtlList = dtlList;
        this.alreadyDtlList = alreadyDtlList;
        this.isBackfill = isBackfill;
    }

    /**
     * 方法描述：添加文本布局
     *
     * @param fileData
     * @return
     */
    public View createTxtView(final String num, final HelpFileData fileData) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        View view = null;
        //1 字符串 2多行滚动文本  3整数  4小数  5百分比
        if (num.equals("1")) {
            view = LayoutInflater.from(context).inflate(R.layout.add_item_txt_layout_7_1, null);//也可以从XML中加载布局
        } else if (num.equals("2")) {
            view = LayoutInflater.from(context).inflate(R.layout.add_item_txt_layout_02, null);//也可以从XML中加载布局
        } else if (num.equals("3")) {
            view = LayoutInflater.from(context).inflate(R.layout.add_item_txt_layout_03, null);//也可以从XML中加载布局
        } else if (num.equals("4")) {
            view = LayoutInflater.from(context).inflate(R.layout.add_item_txt_layout_04, null);//也可以从XML中加载布局
        } else if (num.equals("5")) {
            view = LayoutInflater.from(context).inflate(R.layout.add_item_txt_layout_05, null);//也可以从XML中加载布局
        } else {
            view = LayoutInflater.from(context).inflate(R.layout.add_item_txt_layout_01, null);//也可以从XML中加载布局
        }

        view.setLayoutParams(lp);//设置布局参数

        TextView txt_title = (TextView) view.findViewById(R.id.txt_title);
        TextView tview = (TextView) view.findViewById(R.id.tview);
        final EditText txt_content = (EditText) view.findViewById(R.id.txt_content);

        fileData.setShowType("7");
        txt_title.setText(fileData.getDtlName());
        tview.setVisibility(View.GONE);

        if (!TextUtils.isEmpty(fileData.getDtlValue())) {
            txt_content.setText(fileData.getDtlValue());
            txt_content.setEnabled(false);
        }else{
            if(isBackfill){
                for(HelpFileData data : alreadyDtlList){
                    if(data.getDtlId().equals(fileData.getDtlId())){
                        txt_content.setText(data.getDtlValue());
                        fileData.setDtlValue(data.getDtlValue());
                    }
                }
            }
        }

        txt_content.setHint("请输入" + fileData.getDtlName());
        txt_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                fileData.setDtlValue(txt_content.getText().toString());
            }
        });
        dtlList.add(fileData);
        return view;
    }
}
